import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:slide_popup_dialog/slide_popup_dialog.dart' as slideDialog;
//main(List<String> args) {
//  runApp(Basewhidget());
//}

class Basewhidget extends StatelessWidget {
  // statelessWidget 是一个无状态的组件，用于自定义组件

  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      // materialAPP 是作为根组件使用，定义该页面展示的总体布局
        home: Home()
    );
  }

}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  ScrollController _scrollController = new ScrollController(initialScrollOffset: 0);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("滑动控制"),
      ),
      body: MyHomePage(),
//      回到最上页
//      floatingActionButton: !true
//          ? null
//          : FloatingActionButton(
//        onPressed: () {
//          _scrollController.jumpTo(10);
//        },
//        child: Icon(Icons.arrow_upward),
//      ),

    );
  }
}


class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({Key key, this.title}) : super(key: key);

  @override
  Scontroller1 createState() => Scontroller1();
}



class Scontroller1 extends State<MyHomePage> {
//   获取控制器，用于监听滑动事件
  ScrollController _scrollController = new ScrollController(initialScrollOffset: 0);
  bool isShow = false;
  String _progress = "0%";
  int Nums = 4;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      int offset = _scrollController.position.viewportDimension.toInt();
      print("滑动距离$offset");

      // 如果滑动到底部
      if (_scrollController.offset ==
          _scrollController.position.maxScrollExtent) {
        setState(() {
          print("滑到底部了");
          setState(() {
            this.Nums += 3;   // 动态添加展示数据
          });

          isShow = true;
        });
      } else {
        setState(() {
          isShow = false;
        });
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
//        padding: EdgeInsets.all(20),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Scrollbar(
            child: NotificationListener(
              // ignore: missing_return
                onNotification: (ScrollNotification notification) {
                  //滑动进度/最大可滑动进度
                  double progress = notification.metrics.pixels /
                      notification.metrics.maxScrollExtent;
                  setState(() {
                    _progress = "${((progress * 100).toInt())}%";
                  });
                },
                child: ListView.separated(
                    physics: BouncingScrollPhysics(),
                    controller: _scrollController,
                    itemBuilder: (BuildContext buildContext, int index) {
                      print("index是 $index, Nmus 是$Nums");
                      return Container(     // 在页面中是不会保存这么多图片的，所以，它在滑动后会将上面或下面超过某个范围的
                        // 图片数据清理掉，然后获取新的图片展示 后期可以自己做后端返回url，这样就不会在下滑后随机刷新图片了
                        margin: EdgeInsets.fromLTRB(5, 0, 0, 0),
                        child: Row(
                          children: <Widget>[
                            Expanded(

                              child: Container(
                                height: 200,
                                margin: EdgeInsets.fromLTRB(2, 0, 5, 5),

                                child: Image.network(

                                  // ignore: unnecessary_brace_in_string_interps
                                  "http://api.btstu.cn/sjbz/?lx=m_dongman&a=1&b=${index}",
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                            Expanded(
                              child: Container(
                                margin: EdgeInsets.fromLTRB(5, 5, 0, 5),
                                height: 200,
                                child: Image.network(
                                  // ignore: unnecessary_brace_in_string_interps
                                  "http://api.btstu.cn/sjbz/?lx=m_dongman&a=2&b=${index}",
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),
                            Expanded(
                              child: Container(
                                height: 200,
                                margin: EdgeInsets.fromLTRB(2, 5, 0, 5),
                                child: Image.network(
                                  // ignore: unnecessary_brace_in_string_interps
                                  "http://api.btstu.cn/sjbz/?lx=m_dongman&a=3&b=${index}",
                                  fit: BoxFit.fitHeight,
                                ),
                              ),
                            ),
                          ],
                        ),
                      );
                    },
                    separatorBuilder: (BuildContext buildContext, int index) {
                      return Divider(
                        color: Colors.grey,
                      );
                    },
                    itemCount: this.Nums)),  // 这里是滑动数量
          ),
//            CircleAvatar(
//              //显示进度百分比
//              radius: 30.0,
//              child: Text(_progress),
//              backgroundColor: Colors.black54,
//            ),
        ],
      ),
    );

  }
}

